<!--
 * @Author: YMJ
 * @Date: 2020-10-30 10:27:42
-->
<template>
  <div class="page">
    <div>
      <div class="top flex">
        <span>会员增长</span>
        <div class="tr f1">
          <el-button type="primary" @click="exportList(memberGrow,memberData)">导出数据</el-button>
          <div class="sel">
            <span @click="getMemberGrowth('day')">日</span>
            <span class="br" @click="getMemberGrowth('month')">月</span>
            <span @click="getMemberGrowth('year')">年</span>
          </div>
          <el-date-picker
            v-model="value1"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="getMemberGrowth()"
          />
        </div>
      </div>
      <el-row class="h1 canvas-box">
        <el-col :span="6" class="flex ac pc h100">
          <div class="flex ver ac pc">
            <span class="s1">会员增长数</span>
            <span class="s2">{{ totalNum }}</span>
          </div>
          <!-- <span class="s3">环比<i class="el-icon-caret-top s4"></i>12％</span> -->
        </el-col>
        <el-col class="h100" :span="18">
          <div ref="canvas1" class="h100" />
        </el-col>
      </el-row>
    </div>
    <div style="margin-top: 30px">
      <div class="top flex">
        <span>男女比例</span>
        <div class="tr f1">
          <el-button type="primary" @click="exportList(genderGrow,genderDate)">导出数据</el-button>
          <div class="sel">
            <span @click="getMemberGender('day')">日</span>
            <span class="br" @click="getMemberGender('month')">月</span>
            <span @click="getMemberGender('year')">年</span>
          </div>
          <el-date-picker
            v-model="value2"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="getMemberGender()"
          />
        </div>
      </div>
      <el-row class="h1 canvas-box">
        <el-col :span="6" class="flex ver ac pc h100">
          <div class="flex ac pc">
            <div class="flex ver ac pc">
              <span class="s1">男会员增长数</span>
              <span class="s2">{{ totalMan }}</span>
            </div>
            <!-- <span class="s3">环比<i class="el-icon-caret-top s4"></i>12％</span> -->
          </div>
          <div class="flex ac pc" style="margin-top: 40px">
            <div class="flex ver ac pc">
              <span class="s1">女会员增长数</span>
              <span class="s2">{{ totalWoman }}</span>
            </div>
            <!-- <span class="s3">环比<i class="el-icon-caret-top s4"></i>12％</span> -->
          </div>
        </el-col>
        <el-col class="h100" :span="18">
          <div ref="canvas2" class="h100" />
        </el-col>
      </el-row>
    </div>
    <div style="margin-top: 30px">
      <div class="top flex">
        <span>年龄分布</span>
        <div class="tr f1">
          <el-button type="primary" @click="exportList(ageGrow,ageDate)">导出数据</el-button>
          <div class="sel">
            <span @click="getMemberAge('day')">日</span>
            <span class="br" @click="getMemberAge('month')">月</span>
            <span @click="getMemberAge('year')">年</span>
          </div>
          <el-date-picker
            v-model="value3"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="getMemberAge()"
          />
        </div>
      </div>
      <el-row class="h1 canvas-box">
        <el-col :span="6" class="flex ver ac pc h100">
          <div class="flex ac pc">
            <div class="flex ver ac pc">
              <span class="s1">&lt;18岁会员增长数</span>
              <span class="s2">{{ age1Num }}</span>
            </div>
            <!-- <span class="s3">环比<i class="el-icon-caret-top s4"></i>12％</span> -->
          </div>
          <div class="flex ac pc" style="margin-top: 30px">
            <div class="flex ver ac pc">
              <span class="s1">18岁~40岁会员增长数</span>
              <span class="s2">{{ age2Num }}</span>
            </div>
            <!-- <span class="s3">环比<i class="el-icon-caret-top s4"></i>12％</span> -->
          </div>
          <div class="flex ac pc" style="margin-top: 30px">
            <div class="flex ver ac pc">
              <span class="s1">&gt;18岁会员增长数</span>
              <span class="s2">{{ age3Num }}</span>
            </div>
            <!-- <span class="s3">环比<i class="el-icon-caret-top s4"></i>12％</span> -->
          </div>
        </el-col>
        <el-col class="h100" :span="18">
          <div ref="canvas3" class="h100" />
        </el-col>
      </el-row>
    </div>
    <div style="margin-top: 30px">
      <div class="top flex">
        <span>城市分布</span>
        <div class="tr f1">
          <el-button type="primary" style="margin-right: 20px" @click="exportList(areaGrow,areaData)">导出数据</el-button>
          <el-date-picker
            v-model="value4"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="memberArea()"
          />
        </div>
      </div>
      <el-row class="h2 canvas-box">
        <el-col :span="8" class="flex ver ac pc h100">
          <div v-for="(item, index) in areaList" :key="item.areaProvinceId" class="flex ac pe" style="width: 80%;margin-top: 20px">
            <div class="flex ac box1">
              <i class="c1" :style="{background: item.color ? item.color : color[index]}" />
              <span class="c2">{{ item.name || '未知' }}</span>
            </div>
            <span class="c3">{{ ((item.value / areaTotal)*100).toFixed(2) }}％</span>
            <div class="c4 box1">{{ item.value }}</div>
          </div>
        </el-col>
        <el-col class="h100" :span="16">
          <div ref="canvas4" class="h100" />
          <div class="pos" v-html="areaHtml" />
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import * as echarts from 'echarts'
import { memberGrowth, memberGender, memberAge, memberArea } from '@/api/member'
import { getDay, exportList } from '@/utils/utils'
import { createOption ,memberXlxsHeader} from './config/total.config'


export default {
  data() {
    return {
      ...memberXlxsHeader(),
      value1: '',
      value2: '',
      value3: '',
      value4: '',
      totalNum: 0,
      totalMan: 0,
      totalWoman: 0,
      age1Num: 0,
      age2Num: 0,
      age3Num: 0,
      areaHtml: '',
      areaList: [],
      areaTotal: 0,
      color: ['#1890ff', '#2fc25b', '#facc14', '#f04864', '#8543e0', '#13c2c2'],
      memberData:[],
      genderDate:[],
      ageData:[],
      areaData:[]
    }
  },
  mounted() {
    this.getMemberGrowth('day')
    this.getMemberGender('day')
    this.getMemberAge('day')
    this.memberArea()
  },
  methods: {
    // 导出数据
    exportList(header,data){
      console.log(header,data)
      exportList(header ,data)
    },
    async getMemberGrowth(type) {
      const params = {}
      if (type) params.type = type
      else params.date = this.value1.join(',')
      const [res] = await this.$async(memberGrowth, params)
      // 导出数据后端请求数据
      this.exportData = res.data
      const _xData = {}
      if (type === 'day') {
        [-6, 5, -4, -3, -2, -1, 0].forEach(item => {
          const _time = getDay(item)
          _xData[_time] = 0
        })
      } else if (type === 'month') {
        for (let i = 1; i < 13; i++) {
          const _time = new Date().getFullYear()
          _xData[`${_time}-${i < 10 ? '0' + i : i}`] = 0
        }
      } else if (!type) {
        let num = 0
        while (getDay(num, this.value1[0]) !== this.value1[1]) {
          _xData[getDay(num, this.value1[0])] = 0
          num++
        }
        _xData[ this.value1[1]] = 0
      }
      // 切换选择年月日重置会员增长人数
      this.totalNum = 0
      res.data.forEach(item => {
        this.totalNum += item.value
        _xData[item.key] = item.value
      })
      this.render1(_xData)
    },
    render1(_xData) {
      const option = {
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: Object.keys(_xData)
        },
        yAxis: {
          boundaryGap: true,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed'
            }
          },
          type: 'value'
        },
        series: [{
          lineStyle: {
            color: '#1890FF'
          },
          itemStyle: {
            color: '#1890FF'
          },
          data: Object.keys(_xData).map(item => _xData[item]),
          type: 'line',
          smooth: true,
          areaStyle: {
            color: '#1890FF',
            opacity: 0.3
          }
        }]
      }
      this.renderCanva('canvas1', option)
    },
    async getMemberGender(type) {
      const params = {}
      if (type) params.type = type
      else params.date = this.value2.join(',')
      const [res] = await this.$async(memberGender, params)
      this.genderDate =res.data
      const _xData = {}
      if (type === 'day') {
        [-6, 5, -4, -3, -2, -1, 0].forEach(item => {
          const _time = getDay(item)
          _xData[_time] = {}
        })
      } else if (type === 'month') {
        for (let i = 1; i < 13; i++) {
          const _time = new Date().getFullYear()
          _xData[`${_time}-${i < 10 ? '0' + i : i}`] = {}
        }
      } else if (!type) {
        let num = 0
        while (getDay(num, this.value2[0]) !== this.value2[1]) {
          _xData[getDay(num, this.value2[0])] = {}
          num++
        }
        _xData[ this.value2[1]] = {}
      }
      this.totalMan = 0
      this.totalWoman = 0
      res.data.forEach(item => {
        this.totalMan += item.man
        this.totalWoman += item.woman
        _xData[item.date] = item
      })
      this.render2(_xData)
    },
    render2(_xData) {
      const option = {
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: Object.keys(_xData)
        },
        yAxis: {
          boundaryGap: true,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed'
            }
          },
          type: 'value'
        },
        series: [{
          lineStyle: {
            color: '#1890FF'
          },
          itemStyle: {
            color: '#1890FF'
          },
          data: Object.keys(_xData).map(item => _xData[item].man || 0),
          type: 'line',
          smooth: true,
          areaStyle: {
            color: '#1890FF',
            opacity: 0.3
          }
        }, {
          lineStyle: {
            color: '#2fc25b'
          },
          itemStyle: {
            color: '#2fc25b'
          },
          data: Object.keys(_xData).map(item => _xData[item].woman || 0),
          type: 'line',
          smooth: true,
          areaStyle: {
            color: '#2fc25b',
            opacity: 0.3
          }
        }]
      }
      this.renderCanva('canvas2', option)
    },
    async getMemberAge(type) {
      const params = {}
      if (type) params.type = type
      else params.date = this.value3.join(',')
      const [res] = await this.$async(memberAge, params)
      this.ageData = res.data
      const _xData = {}
      if (type === 'day') {
        [-6, -5, -4, -3, -2, -1, 0].forEach(item => {
          const _time = getDay(item)
          _xData[_time] = {}
        })
      } else if (type === 'month') {
        for (let i = 1; i < 13; i++) {
          const _time = new Date().getFullYear()
          _xData[`${_time}-${i < 10 ? '0' + i : i}`] = {}
        }
      } else if (!type) {
        let num = 0
        while (getDay(num, this.value3[0]) !== this.value3[1]) {
          _xData[getDay(num, this.value3[0])] = {}
          num++
        }
        _xData[ this.value3[1]] = {}
      }
      this.age1Num = this.age2Num = this.age3Num = 0
      res.data.forEach(item => {
        this.age1Num += item.age18
        this.age2Num += item.age1840
        this.age3Num += item.age40
        _xData[item.date] = item
      })
      this.render3(_xData)
    },
    render3(_xData) {
      const option = {
        xAxis: {
          type: 'category',
          data: Object.keys(_xData)
        },
        yAxis: {
          boundaryGap: true,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed'
            }
          },
          type: 'value'
        },
        series: [{
          itemStyle: {
            color: '#3aa1ff'
          },
          data: Object.keys(_xData).map(item => _xData[item].age18 || 0),
          type: 'bar'
        }, {
          itemStyle: {
            color: '#4ecb73'
          },
          data: Object.keys(_xData).map(item => _xData[item].age1840 || 0),
          type: 'bar'
        }, {
          itemStyle: {
            color: '#fbd437'
          },
          data: Object.keys(_xData).map(item => _xData[item].age40 || 0),
          type: 'bar'
        }]
      }
      this.renderCanva('canvas3', option)
    },
    async memberArea() {
      const params = {}
      if (this.value4) params.date = this.value4.join(',')
      const [res] = await this.$async(memberArea, params)
      this.areaData = res.data
      const data1 = []
      let _total = 0
      if (res.data.length === 0) {
        res.data = [{ total: 1, areaProvinceName: '未知', color: '#eeeeee' }]
      }
      res.data.forEach((item, index) => {
        if (index > 4) {
          const _data = data1[5] || {}
          data1[5] = { value: item.total + (_data.value || 0), name: '其他', itemStyle: { color: item.color ? item.color : this.color[index], borderRadius: 10 }}
        } else {
          data1.push({ value: item.total, name: item.areaProvinceName, itemStyle: { color: item.color ? item.color : this.color[index], borderRadius: 10 }})
        }
        _total += item.total
      })
      this.areaTotal = _total
      this.areaList = data1
      this.areaHtml = `<p style="margin: 0;">总人数</p><p style="margin: 0;color: #454545;font-size: 18px;">${_total}</p>`
      this.renderCanva('canvas4', createOption(data1))
    },
    renderCanva(name, option) {
      const myChart = echarts.init(this.$refs[name])
      myChart.setOption(option)
    }
  }
}
</script>

<style  scoped>
.s1{
  color: rgba(0, 0, 0, 0.42745);
  font-size: 14px;
}
.s2{
  color: rgba(0, 0, 0, 0.847058);
  font-size: 24px;
  margin-top: 10px;
}
.s3{
  color: rgba(0, 0, 0, 0.42745);
  font-size: 14px;
  margin-left: 40px;
}
.s4{
  color: #2fc25b;
  font-size: 18px;
}
.box1{
  height: 25px;
  border-right: 1px solid rgba(0, 0, 0, 0.2);
}
.c1{
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: red;
  margin-right: 50px;
}
.c2{
  color: rgba(0, 0, 0, 0.647058);
  font-size: 13px;
  padding-right: 20px;
}
.c3{
  color: rgba(0, 0, 0, 0.42745);
  font-size: 13px;
  /* margin-left: 30px;
  margin-right: 80px; */
}
.c4{
  color: rgba(0, 0, 0, 0.647058);
  font-size: 13px;
  padding-right: 20px;
  line-height: 25px;
}
.tr /deep/ .el-range-editor.el-input__inner{
    width: 260px!important;
}
.tr /deep/ .el-input__icon.el-range__close-icon{
    display: none;
}
.h100{
    height: 100%;
    position: relative;
}
.pos{
    position: absolute;
    z-index: 9;
    left: 50%;
    top: 50%;
    text-align: center;
    transform: translate(-50%, -50%);
    color: #828282;
    font-size: 14px;
}
.el-button{
    height: 34px;
    line-height: 34px;
    padding: 0 10px;
    box-sizing: border-box;
}
.flex{
    display: flex;
}
.ac{
  align-items: center;
}
.pc{
  justify-content: center;
}
.pe{
  justify-content: space-around;
}
.ver{
  flex-direction: column;
}
.f1{
    flex: 1;
}
.page{
    padding: 15px;
    background: #FFFFFF;
}
.top{
    box-sizing: border-box;
    padding: 0 15px;
    height: 50px;
    line-height: 50px;
    background-color: #f2f2f2;
    border: 1px solid #e9e9e9;
}
.tr{
    text-align: right;
}
.sel{
    box-sizing: border-box;
    display: inline-block;
    border: 1px solid #1890ff;
    color: #1890ff;
    border-radius: 5px;
    height: 34px;
    line-height: 34px;
    margin: 0 20px 0 40px;
}
.sel span{
    padding: 0 20px;
    display: inline-block;
    height: 100%;
    cursor:pointer;
}
.br{
    border-left: 1px solid #1890ff;
    border-right: 1px solid #1890ff;
}
.canvas-box{
    border: 1px solid #e9e9e9;
    border-top: none;
    box-sizing: border-box;
    padding: 0 20px;
}
.h1{
  height: 300px;
}
.h2{
  height: 500px;
}
</style>
